package com.yiren.manager.ctrl.assets;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrAsset;
import com.yiren.entity.YrRoom;
import com.yiren.entity.YrRoomAsset;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.assets.YrAssetForm;
import com.yiren.manager.form.assets.YrBaseAssetForm;
import com.yiren.manager.form.assets.YrBuildingAssetForm;
import com.yiren.manager.form.assets.YrFloorsAssetForm;
import com.yiren.manager.form.assets.YrRoomAssetForm;
import com.yiren.manager.form.assets.YrRoomBaseAssetForm;
import com.yiren.manager.service.iface.assets.YrAssetService;
import com.yiren.manager.service.iface.assets.YrRoomAssetService;
import com.yiren.manager.service.iface.beds.YrRoomService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("roomAsset")
public class YrRoomAssetController extends GenericController<YrRoomAssetForm, YrRoomAsset>{

	@Autowired
	private YrRoomAssetService selfService;
	
	@Autowired
	private YrAssetService assetService;
	
	@Autowired
	private YrRoomService roomService;

	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrRoomAssetForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrRoomAssetForm> result = new HTTPResult<YrRoomAssetForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrRoomAssetForm form = tool.mapper("roomAsset", YrRoomAssetForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getRoomAssetId()||form.getRoomAssetId().isEmpty()){
			form.setRoomAssetId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		String sqlWhere = "room_id=?1 AND asset_id=?2";
		QueryResult<YrRoomAsset> qr = selfService.find(YrRoomAsset.class, sqlWhere, new Object[]{form.getRoomId(),form.getAssetId()});
		
		if(qr.getTotalRecord()>0){
			form.setRoomAssetId(qr.getResultList().get(0).getRoomAssetId());
			form.setQuantity(qr.getResultList().get(0).getQuantity()+form.getQuantity())	;
		}
		
		form.setUpdateTime(new Date());
		YrRoomAsset entity = ObjectUtils.objectToObject(form, YrRoomAsset.class);
		selfService.save(entity);
		
		return result;
	}
	

	@RequestMapping("findByRoomId")
	@ResponseBody
	public HTTPResult<YrRoomAssetForm> findByRoomId(String roomId) throws Exception{
		HTTPResult<YrRoomAssetForm> result = new HTTPResult<YrRoomAssetForm>();
		String sqlWhere = "room_id=?1";
		QueryResult<YrRoomAsset> qr = selfService.find(YrRoomAsset.class, sqlWhere, new Object[]{roomId});
		
		List<YrRoomAssetForm> formList = new ArrayList<YrRoomAssetForm>();
		for(YrRoomAsset entity : qr.getResultList()){
			YrRoomAssetForm form = toForm(entity);
			YrAsset asset = assetService.findOne(form.getAssetId());
			if(null!=asset){
				form.setAssetForm(ObjectUtils.objectToObject(asset, YrAssetForm.class));
			}
			formList.add(form);
		}
		result.setData(formList);
		
		return result;
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrBaseAssetForm> findAll() throws Exception{
		HTTPResult<YrBaseAssetForm> result = new HTTPResult<YrBaseAssetForm>();
		List<YrRoomAsset> list = selfService.findAll();
		
		Map<String, YrBaseAssetForm> map = new HashMap<String, YrBaseAssetForm>();
		for(YrRoomAsset entity : list){
			YrBaseAssetForm form = null;
			if(map.containsKey(entity.getAssetId())){
				form = map.get(entity.getAssetId());
				Integer qua = form.getQuantity()+entity.getQuantity();
				form.setQuantity(qua);
			}else {
				form = new YrBaseAssetForm();
				form.setAssetId(entity.getAssetId());
				form.setQuantity(entity.getQuantity());
				YrAsset asset = assetService.findOne(form.getAssetId());
				if(null!=asset){
					form.setAssetName(asset.getAssetName());
					form.setPrice(asset.getPrice());
				}
			}
			map.put(entity.getAssetId(), form);
		}
		List<YrBaseAssetForm> formList = new ArrayList<YrBaseAssetForm>();
		for(Map.Entry<String, YrBaseAssetForm> entry : map.entrySet()){
			formList.add(entry.getValue());
		}
		
		result.setData(formList);
		return result;
	}
	
	@RequestMapping("findBuilding")
	@ResponseBody
	public 	HTTPResult<YrBuildingAssetForm> findBuilding() throws Exception{
		HTTPResult<YrBuildingAssetForm> result = new HTTPResult<YrBuildingAssetForm>();
		List<YrRoomAsset> list = selfService.findAll();
		
		Map<String, YrBuildingAssetForm> buildingMap = new HashMap<String, YrBuildingAssetForm>();
		
		//按楼房分组
		for(YrRoomAsset roomAsset : list){
			if(buildingMap.containsKey(roomAsset.getBuildingId())){
				List<YrBaseAssetForm> baseAssetForms = buildingMap.get(roomAsset.getBuildingId()).getBaseAssetForms();
				boolean flag = false;
				for(YrBaseAssetForm baseAssetForm : baseAssetForms){
					if(roomAsset.getAssetId()==baseAssetForm.getAssetId()){
						baseAssetForm.setQuantity(baseAssetForm.getQuantity()+roomAsset.getQuantity());
						flag = true;
					}
				}
				if(!flag){//如果该资产在列表中没有
					YrBaseAssetForm baseAssetForm = new YrBaseAssetForm();
					baseAssetForm.setAssetId(roomAsset.getAssetId());
					baseAssetForm.setAssetName(roomAsset.getAssetName());
					baseAssetForm.setQuantity(roomAsset.getQuantity());
					YrAsset asset = assetService.findOne(roomAsset.getAssetId());
					if(null!=asset){
						baseAssetForm.setPrice(asset.getPrice());
					}
					buildingMap.get(roomAsset.getBuildingId()).getBaseAssetForms().add(baseAssetForm);
				}
			}else {
				YrBuildingAssetForm buildingAssetForm = new YrBuildingAssetForm();
				buildingAssetForm.setBuildingId(roomAsset.getBuildingId());
				buildingAssetForm.setBuildingName(roomAsset.getBuildingName());
				
				YrBaseAssetForm baseAssetForm = new YrBaseAssetForm();
				baseAssetForm.setAssetId(roomAsset.getAssetId());
				baseAssetForm.setAssetName(roomAsset.getAssetName());
				baseAssetForm.setQuantity(roomAsset.getQuantity());
				
				YrAsset asset = assetService.findOne(roomAsset.getAssetId());
				if(null!=asset){
					baseAssetForm.setPrice(asset.getPrice());
				}
				List<YrBaseAssetForm> baseAssetForms = new ArrayList<YrBaseAssetForm>();
				baseAssetForms.add(baseAssetForm);
				buildingAssetForm.setBaseAssetForms(baseAssetForms);
				buildingMap.put(roomAsset.getBuildingId(), buildingAssetForm);
			}
		}
		
		//按楼层分组
		for(Map.Entry<String, YrBuildingAssetForm> entry : buildingMap.entrySet()){
			YrBuildingAssetForm buildingAssetForm = entry.getValue();
			Map<String, YrFloorsAssetForm> floorMap = new HashMap<String, YrFloorsAssetForm>();

			for(YrRoomAsset roomAsset : list){
				if(floorMap.containsKey(roomAsset.getFloorsId())){
					List<YrBaseAssetForm> baseAssetForms = floorMap.get(roomAsset.getFloorsId()).getBaseAssetForms();
					boolean flag = false;
					for(YrBaseAssetForm baseAssetForm : baseAssetForms){
						if(roomAsset.getAssetId()==baseAssetForm.getAssetId()){
							baseAssetForm.setQuantity(baseAssetForm.getQuantity()+roomAsset.getQuantity());
							flag = true;
						}
					}
					if(!flag){//如果该资产在列表中没有
						YrBaseAssetForm baseAssetForm = new YrBaseAssetForm();
						baseAssetForm.setAssetId(roomAsset.getAssetId());
						baseAssetForm.setAssetName(roomAsset.getAssetName());
						baseAssetForm.setQuantity(roomAsset.getQuantity());
						YrAsset asset = assetService.findOne(roomAsset.getAssetId());
						if(null!=asset){
							baseAssetForm.setPrice(asset.getPrice());
						}
						floorMap.get(roomAsset.getFloorsId()).getBaseAssetForms().add(baseAssetForm);
					}
				}else {
					YrFloorsAssetForm floorsAssetForm = new YrFloorsAssetForm();
					floorsAssetForm.setFloorsId(roomAsset.getFloorsId());
					floorsAssetForm.setFloorsName(roomAsset.getFloorsName());
					
					YrBaseAssetForm baseAssetForm = new YrBaseAssetForm();
					baseAssetForm.setAssetId(roomAsset.getAssetId());
					baseAssetForm.setAssetName(roomAsset.getAssetName());
					baseAssetForm.setQuantity(roomAsset.getQuantity());
					
					YrAsset asset = assetService.findOne(roomAsset.getAssetId());
					if(null!=asset){
						baseAssetForm.setPrice(asset.getPrice());
					}
					List<YrBaseAssetForm> baseAssetForms = new ArrayList<YrBaseAssetForm>();
					baseAssetForms.add(baseAssetForm);
					floorsAssetForm.setBaseAssetForms(baseAssetForms);
					floorMap.put(roomAsset.getFloorsId(), floorsAssetForm);
				}
				List<YrFloorsAssetForm> floorsAssetForms = new ArrayList<YrFloorsAssetForm>();
				for(Map.Entry<String, YrFloorsAssetForm> floorEntry : floorMap.entrySet()){
					floorsAssetForms.add(floorEntry.getValue());
				}
				buildingAssetForm.setFloorsAssetForms(floorsAssetForms);
			}
			for(YrFloorsAssetForm floorsAssetForm : buildingAssetForm.getFloorsAssetForms()){
				
//				String sqlWhere = "floors_id=?1";
//				QueryResult<YrRoomAsset> qr = selfService.find(YrRoomAsset.class, sqlWhere, new Object[]{floorsAssetForm.getFloorsId()});
				
				List<YrRoomAsset> roomAssetList = new ArrayList<YrRoomAsset>();
				for(YrRoomAsset roomAsset : list){
					if(roomAsset.getFloorsId().equals(floorsAssetForm.getFloorsId())){
						roomAssetList.add(roomAsset);
					}
				}
				Map<String, List<YrRoomAssetForm>> map = new HashMap<String, List<YrRoomAssetForm>>();
				
				for(YrRoomAsset roomAsset : roomAssetList){
			
					if(map.containsKey(roomAsset.getRoomId())){
						 map.get(roomAsset.getRoomId()).add(toForm(roomAsset));
					}else {
						List<YrRoomAssetForm> roomAssetForms = new ArrayList<YrRoomAssetForm>();
						roomAssetForms.add(toForm(roomAsset));
						map.put(roomAsset.getRoomId(), roomAssetForms);
					}
				}
				
				List<YrRoomBaseAssetForm> roomBaseAssetForms = new ArrayList<YrRoomBaseAssetForm>();
				for(Map.Entry<String, List<YrRoomAssetForm>> entry2 : map.entrySet()){
					YrRoom room = roomService.findOne(entry2.getKey());
					YrRoomBaseAssetForm roomBaseAssetForm = new YrRoomBaseAssetForm();
					if(null!=room){
						roomBaseAssetForm.setRoomId(entry2.getKey());
						roomBaseAssetForm.setRoomNumber(room.getRoomNumber());
						roomBaseAssetForm.setRoomType(room.getRoomType());
						roomBaseAssetForm.setRoomAssetForms(entry2.getValue());
					}
					roomBaseAssetForms.add(roomBaseAssetForm);
				}
				floorsAssetForm.setRoomBaseAssetForms(roomBaseAssetForms);
			}
		}
		
		List<YrBuildingAssetForm> formList = new ArrayList<YrBuildingAssetForm>();
		for(Map.Entry<String, YrBuildingAssetForm> entry : buildingMap.entrySet()){
			formList.add(entry.getValue());
		}
		
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrRoomAssetForm toForm(YrRoomAsset e) throws Exception {
		YrRoomAssetForm form = ObjectUtils.objectToObject(e, YrRoomAssetForm.class);
		return form;
	}

	@Override
	public YrRoomAsset toEntity(YrRoomAssetForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
